package cn.cdu.medical.controller;

import cn.cdu.basics.baseVo.Result;
import cn.cdu.basics.utils.ResultUtil;
import cn.cdu.medical.dto.ConsultationRequest;
import cn.cdu.medical.dto.AssistantRequest;
import cn.cdu.medical.entity.Consultation;
import cn.cdu.medical.service.ConsultationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

@Slf4j
@RestController
@Tag(name = "智能问诊接口")
@RequestMapping("/zwz/consultation")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ConsultationController {

    @Autowired
    private ConsultationService consultationService;

    @PostConstruct
    public void init() {
        System.out.println("ConsultationController 初始化完成");
    }
    @GetMapping("/getOne")
    @Operation(summary = "查询单条问诊记录")
    public Result<Consultation> getOne(@RequestParam String id) {
        return new ResultUtil<Consultation>().setData(consultationService.getById(id));
    }

    @PostMapping("/chat")
    @Operation(summary = "智能问诊对话")
    public Result<Object> chat(@RequestBody ConsultationRequest request) {
        try {
            Consultation consultation = consultationService.chat(request);
            java.util.Map<String, Object> result = new java.util.HashMap<>();
            result.put("result", consultation.getResponse());
            result.put("consultationId", consultation.getId());
            log.info("Controller返回consultationId: {}", consultation.getId());
            return new ResultUtil<>().setData(result);
        } catch (Exception e) {
            log.error("智能问诊对话失败", e);
            return new ResultUtil<>().setErrorMsg("问诊请求失败，请稍后重试");
        }
    }

    @PostMapping("/assistant/chat")
    @Operation(summary = "智能助手对话")
    public Result<String> assistantChat(@RequestBody AssistantRequest request) {
        try {
            log.info("智能助手对话请求: {}", request);
            String response = consultationService.assistantChat(request);
            return new ResultUtil<String>().setData(response);
        } catch (Exception e) {
            log.error("智能助手对话失败", e);
            return new ResultUtil<String>().setErrorMsg("请求失败，请稍后重试");
        }
    }

    @PostMapping("/assistant/analyze")
    @Operation(summary = "病历分析")
    public Result<String> analyzeCase(@RequestBody AssistantRequest request) {
        try {
            log.info("病历分析请求: {}", request);
            String response = consultationService.analyzeCase(request);
            return new ResultUtil<String>().setData(response);
        } catch (Exception e) {
            log.error("病历分析失败", e);
            return new ResultUtil<String>().setErrorMsg("分析失败，请稍后重试");
        }
    }

    @PostMapping("/contraindications")
    @Operation(summary = "用药禁忌查询")
    public Result<String> checkContraindications(@RequestBody AssistantRequest request) {
        try {
            log.info("用药禁忌查询请求: {}", request);
            String response = consultationService.checkContraindications(request);
            return new ResultUtil<String>().setData(response);
        } catch (Exception e) {
            log.error("用药禁忌查询失败", e);
            return new ResultUtil<String>().setErrorMsg("查询失败，请稍后重试");
        }
    }

    @PostMapping("/medicine-guide")
    @Operation(summary = "药品说明书查询")
    public Result<String> getMedicineGuide(@RequestBody AssistantRequest request) {
        try {
            log.info("药品说明书查询请求: {}", request);
            String response = consultationService.getMedicineGuide(request);
            return new ResultUtil<String>().setData(response);
        } catch (Exception e) {
            log.error("药品说明书查询失败", e);
            return new ResultUtil<String>().setErrorMsg("查询失败，请稍后重试");
        }
    }

    @PostMapping("/clinical-guide")
    @Operation(summary = "诊疗指南查询")
    public Result<String> getClinicalGuide(@RequestBody AssistantRequest request) {
        try {
            log.info("诊疗指南查询请求: {}", request);
            String response = consultationService.getClinicalGuide(request);
            return new ResultUtil<String>().setData(response);
        } catch (Exception e) {
            log.error("诊疗指南查询失败", e);
            return new ResultUtil<String>().setErrorMsg("查询失败，请稍后重试");
        }
    }
} 